package com.example.wananrdroid.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wananrdroid.R;
import com.example.wananrdroid.bean.NaviBean;
import com.example.wananrdroid.bean.TreeBean;
import com.example.wananrdroid.fragment.zhufragment.tixi.FlowLayout;

import java.util.List;

public class DaohangAdapter extends RecyclerView.Adapter<DaohangAdapter.ViewHolder> {
    private Context context;
    private List<NaviBean.DataBean> data;
    private OnContextClickListener onContextClickListener;
    public interface OnContextClickListener{
        void oncontextclicklistener(View v, int position,int i);
    }

    public void setOnContextClickListener(OnContextClickListener onContextClickListener) {
        this.onContextClickListener = onContextClickListener;
    }

    public DaohangAdapter(Context context, List<NaviBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.tree_tiem, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        List<NaviBean.DataBean.ArticlesBean> articles = data.get(position).getArticles();
        for (int i = 0; i < articles.size(); i++) {
            int i1=i;
            String name = articles.get(i).getTitle();
            View inflate = LayoutInflater.from(context).inflate(R.layout.fl_item, null);
            TextView name1 = inflate.findViewById(R.id.fl_name);
            name1.setText(name);
            holder.fl.addView(inflate);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onContextClickListener.oncontextclicklistener(view,position,i1);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        FlowLayout fl;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            fl=itemView.findViewById(R.id.fl);
        }
    }
}
